<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8"/>
    <link href="/images/favicon.ico" rel="icon">
    <title>用户注册</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/libs/layui/css/layui.css"/>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/module/admin.css?v=318">
    <style>
        body {
            background-image: url("${pageContext.request.contextPath}/static/images/3.jpg");
            background-repeat: no-repeat;
            background-size: cover;
            min-height: 100vh;
        }

        body:before {
            content: "";
            background-color: rgba(0, 0, 0, .2);
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
        }

        .login-wrapper {
            max-width: 420px;
            padding: 20px;
            margin: 0 auto;
            position: relative;
            box-sizing: border-box;
            z-index: 2;
        }

        .login-wrapper > .layui-form {
            padding: 25px 30px;
            background-color: #fff;
            box-shadow: 0 3px 6px -1px rgba(0, 0, 0, 0.19);
            box-sizing: border-box;
            border-radius: 4px;
        }

        .login-wrapper > .layui-form > h2 {
            color: #333;
            font-size: 18px;
            text-align: center;
            margin-bottom: 25px;
        }

        .login-wrapper > .layui-form > .layui-form-item {
            margin-bottom: 25px;
            position: relative;
        }

        .login-wrapper > .layui-form > .layui-form-item:last-child {
            margin-bottom: 0;
        }

        .login-wrapper > .layui-form > .layui-form-item > .layui-input {
            height: 46px;
            line-height: 46px;
            border-radius: 2px !important;
        }

        .login-wrapper .layui-input-icon-group > .layui-input {
            padding-left: 46px;
        }

        .login-wrapper .layui-input-icon-group > .layui-icon {
            width: 46px;
            height: 46px;
            line-height: 46px;
            font-size: 20px;
            color: #909399;
            position: absolute;
            left: 0;
            top: 0;
            text-align: center;
        }

        .login-wrapper > .layui-form > .layui-form-item.login-captcha-group {
            padding-right: 135px;
        }

        .login-wrapper > .layui-form > .layui-form-item.login-captcha-group > .login-captcha {
            height: 46px;
            width: 120px;
            cursor: pointer;
            box-sizing: border-box;
            border: 1px solid #e6e6e6;
            border-radius: 2px !important;
            position: absolute;
            right: 0;
            top: 0;
        }

        .login-wrapper > .layui-form > .layui-form-item > .layui-form-checkbox {
            margin: 0 !important;
            padding-left: 25px;
        }

        .login-wrapper > .layui-form > .layui-form-item > .layui-form-checkbox > .layui-icon {
            width: 15px !important;
            height: 15px !important;
        }

        .login-wrapper > .layui-form .layui-btn-fluid {
            height: 48px;
            line-height: 48px;
            font-size: 16px;
            border-radius: 2px !important;
        }

        .login-wrapper > .layui-form > .layui-form-item.login-oauth-group > a > .layui-icon {
            font-size: 26px;
        }

        .login-copyright {
            color: #eee;
            padding-bottom: 20px;
            text-align: center;
            position: relative;
            z-index: 1;
        }

        @media screen and (min-height: 550px) {
            .login-wrapper {
                margin: -250px auto 0;
                position: absolute;
                top: 50%;
                left: 0;
                right: 0;
                width: 100%;
            }

            .login-copyright {
                position: absolute;
                bottom: 0;
                right: 0;
                left: 0;
            }
        }

        .layui-btn {
            background-color: #5FB878;
            border-color: #5FB878;
        }

        .layui-link {
            color: #5FB878 !important;
        }

        #tips, #tips2 {
            left: 170px;
            font-size: 6px;
            width: 200px;
            z-index: 8;
        }
    </style>
</head>
<body>
<div class="login-wrapper layui-anim layui-anim-scale layui-hide">
    <form class="layui-form">
        <h2>用户注册</h2>
        <div class="layui-form-item layui-input-icon-group">
            <i class="layui-icon layui-icon-username"></i>
            <input class="layui-input" name="userName" placeholder="请输入登录账号" autocomplete="off"
                   lay-verType="tips" lay-verify="required" id="userName" required/>
            <i class="layui-icon" id="tips">&nbsp;</i>
        </div>
        <div class="layui-form-item layui-input-icon-group">
            <i class="layui-icon layui-icon-email"></i>
            <input class="layui-input" name="email" placeholder="请输入个人邮箱" autocomplete="off"
                   lay-verType="tips" lay-verify="email" id="email"
                   type="email" required email/>
            <i class="layui-icon" id="tips2">&nbsp;</i>
        </div>
        <div class="layui-form-item layui-input-icon-group">
            <i class="layui-icon layui-icon-password"></i>
            <input class="layui-input" name="password" placeholder="请输入登录密码" type="password"
                   lay-verType="tips" lay-verify="required" required/>
        </div>
        <div class="layui-form-item layui-input-icon-group">
            <i class="layui-icon layui-icon-key"></i>
            <input class="layui-input" name="password2" placeholder="请再次输入登录密码" type="password"
                   lay-verType="tips" lay-verify="equalTo" lay-equalTo="[name=password]" required/>
        </div>
        <div class="layui-form-item layui-input-icon-group">
            <i class="layui-icon layui-icon-user" style="z-index: 1000"></i>
            <select name="roleId" lay-verType="tips" lay-verify="required" style="height: 46px" required>
                <option value="">          用户身份</option>
                <option value="3">租客</option>
                <option value="2">房东</option>
            </select>
        </div>
        <div class="layui-form-item layui-input-icon-group login-captcha-group">
            <i class="layui-icon layui-icon-auz"></i>
            <input class="layui-input" name="captcha" placeholder="请输入验证码" autocomplete="off"
                   lay-verType="tips" lay-verify="required" required/>
            <img class="login-captcha" src="${pageContext.request.contextPath}/captcha/getCaptcha?"
            <%--'&time='+new Date().getDate() 获取当前时间 改变src的值 如果src值不改变,那么不会发送重复的请求 --%>
                 onclick="this.src=this.src+'&time='+new Date().getDate()">
        </div>
        <div class="layui-form-item">
            <a href="${pageContext.request.contextPath}/jsp/login.jsp" class="layui-link">返回登录</a>
            <a href="${pageContext.request.contextPath}/backjsp/forget.jsp" class="layui-link pull-right">忘记密码</a>
        </div>
        <div class="layui-form-item" style="margin-bottom: 20px;">
            <button class="layui-btn layui-btn-fluid" lay-filter="loginSubmit" lay-submit>注册</button>
        </div>
    </form>
</div>

<script type="text/javascript" src="${pageContext.request.contextPath}/static/libs/layui/layui.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/js/common.js?v=318"></script>
<script>
    layui.use(['layer', 'form', 'formX'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        $('.login-wrapper').removeClass('layui-hide');
        //判断用户名是否存在
        var userNameCode;
        var emailCode;

        function userNameCheck() {
            $.ajaxSetup({async: false});
            var info = $('#userName').val()
            //发送ajax  请求地址 参数 请求方式
            if (info != '') {
                $.ajax({
                    url: '${pageContext.request.contextPath}/user/registerCheck',
                    data: {userName: info},
                    dataType: 'json',
                    type: 'post',
                    success: function (res) {
                        if (res.code == 0) {
                            //设置
                            $('#tips').css({
                                color: "green",
                            }).attr({
                                class: "layui-icon layui-icon-ok"
                            }).html(res.msg);
                        } else {
                            $('#tips').css({
                                color: "#EDA200",
                            }).attr({
                                class: "layui-icon layui-icon-close"
                            }).html(res.msg);
                        }
                        userNameCode = res.code
                    },
                    error: function () {
                        layer.msg('用户名验证请求失败');
                    }
                })
            }

        }

        function emailCheck() {
            $.ajaxSetup({async: false});
            var info = $('#email').val()
            //发送ajax  请求地址 参数 请求方式
            $.ajax({
                url: '${pageContext.request.contextPath}/user/emailCheck',
                data: {email: info},
                dataType: 'json',
                type: 'post',
                success: function (res) {
                    if (res.code == 0) {
                        //设置
                        $('#tips2').css({
                            color: "green",
                        }).attr({
                            class: "layui-icon layui-icon-ok"
                        }).html(res.msg);
                    } else {
                        $('#tips2').css({
                            color: "#EDA200",
                        }).attr({
                            class: "layui-icon layui-icon-close"
                        }).html(res.msg);
                    }
                    emailCode = res.code
                },
                error: function () {
                    layer.msg('用户名验证请求失败');
                }
            })
        }

        /* 表单提交 */
        form.on('submit(loginSubmit)', function (obj) {
            userNameCheck();
            emailCheck();
            if (userNameCode == "0" && emailCode == "0") {
                $.post('${pageContext.request.contextPath}/user/register', obj.field, function (res) {
                    if (res.code === 0) {
                        layer.msg('注册成功', {icon: 1, time: 1500}, function () {
                            location.href = 'login.jsp';
                        });
                    } else {
                        layer.msg(res.msg, {icon: 2, anim: 6});
                    }
                }, 'json');
            }
            return false
        });
    });
</script>
</body>
</html>
